package henu.javaweb.servlet;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import henu.javaweb.action.ITeacherDao;
import henu.javaweb.bean.Teacher;
import henu.javaweb.dao.DaoFactory;
import henu.javaweb.util.MD5;



/**

 * Servlet implementation class AdminLoginServlet

 */
@WebServlet("/AdminLoginServlet")
public class AdminLoginServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub

		doPost(request, response);
	}

	
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		response.setCharacterEncoding("UTF-8");
		response.setContentType("text/html;Charset=utf-8");
		request.setCharacterEncoding("utf-8");
		PrintWriter out=response.getWriter();
		
		String aid=request.getParameter("aid");
		String apassword=request.getParameter("apassword");
		ITeacherDao td=DaoFactory.getTeacherDaoInstance();
		if(td.Admin()) {
			Teacher t=td.login(aid, apassword);
			if(t!=null) {
			    if(t.getT_isAdmin()==true) {
				request.getSession().setAttribute("Admin",t);
				request.getSession().setAttribute("teacher", t);
				response.sendRedirect("jsp/admin/main.jsp");
				}
			    else {
			    	out.print("<script type=\"text/javascript\">alert('您不是管理员!');history.go(-1);</script>");
			    }
			}
			else {
				out.print("<script type=\"text/javascript\">alert('账号或密码有误!');history.go(-1);</script>");
			}

		}
		else {
			if(aid.equals("admin")&&apassword.equals("admin")) {
				response.sendRedirect("jsp/admin/main.jsp");
				request.getSession().setAttribute("tname", "admin");
			}
			else {
				out.print("<script type=\"text/javascript\">alert('还未设置管理员，请用初始密码登录!');history.go(-1);</script>");
			}
		}
	}

}